/**
 * $Id: RectangleShape.java,v 1.5 2012-01-13 13:12:37 david Exp $
 * Copyright (c) 2007-2012, JGraph Ltd
 */
package com.kg.rails.component.editor.shape;

import com.kg.rails.component.editor.canvas.Graphics2DCanvas;
import com.kg.rails.component.editor.swing.util.mxSwingConstants;
import com.kg.rails.component.editor.util.Constants;
import com.kg.rails.component.editor.util.EditorUtils;
import com.kg.rails.component.editor.view.CellState;

import java.awt.*;
import java.util.Map;

public class RectangleShape extends BasicShape {

    /**
     *
     */
    public void paintShape(Graphics2DCanvas canvas, CellState state) {
        Map<String, Object> style = state.getStyle();

        if (EditorUtils.isTrue(style, Constants.STYLE_ROUNDED, false)) {
            Rectangle tmp = state.getRectangle();

            int x = tmp.x;
            int y = tmp.y;
            int w = tmp.width;
            int h = tmp.height;
            int radius = getArcSize(w, h);

            boolean shadow = hasShadow(canvas, state);
            int shadowOffsetX = (shadow) ? Constants.SHADOW_OFFSETX : 0;
            int shadowOffsetY = (shadow) ? Constants.SHADOW_OFFSETY : 0;

            if (canvas.getGraphics().hitClip(x, y, w + shadowOffsetX,
                    h + shadowOffsetY)) {
                // Paints the optional shadow
                if (shadow) {
                    canvas.getGraphics().setColor(mxSwingConstants.SHADOW_COLOR);
                    canvas.getGraphics().fillRoundRect(
                            x + Constants.SHADOW_OFFSETX,
                            y + Constants.SHADOW_OFFSETY, w, h, radius,
                            radius);
                }

                // Paints the background
                if (configureGraphics(canvas, state, true)) {
                    canvas.getGraphics().fillRoundRect(x, y, w, h, radius,
                            radius);
                }

                // Paints the foreground
                if (configureGraphics(canvas, state, false)) {
                    canvas.getGraphics().drawRoundRect(x, y, w, h, radius,
                            radius);
                }
            }
        } else {
            Rectangle rect = state.getRectangle();

            // Paints the background
            if (configureGraphics(canvas, state, true)) {
                canvas.fillShape(rect, hasShadow(canvas, state));
            }

            // Paints the foreground
            if (configureGraphics(canvas, state, false)) {
                canvas.getGraphics().drawRect(rect.x, rect.y, rect.width,
                        rect.height);
            }
        }
    }

    /**
     * Computes the arc size for the given dimension.
     *
     * @param w Width of the rectangle.
     * @param h Height of the rectangle.
     * @return Returns the arc size for the given dimension.
     */
    public int getArcSize(int w, int h) {
        int arcSize;

        if (w <= h) {
            arcSize = (int) Math.round(h
                    * Constants.RECTANGLE_ROUNDING_FACTOR);

            if (arcSize > (w / 2)) {
                arcSize = w / 2;
            }
        } else {
            arcSize = (int) Math.round(w
                    * Constants.RECTANGLE_ROUNDING_FACTOR);

            if (arcSize > (h / 2)) {
                arcSize = h / 2;
            }
        }
        return arcSize;
    }

}
